package com.example.smartcitydemo.ui

import android.content.IntentFilter
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseActivity
import com.example.smartcitydemo.databinding.ActivityMainBinding
import com.example.smartcitydemo.receiver.LoginBroadCastReceiver

class MainActivity : AppCompatActivity() {

    val loginReceiver: LoginBroadCastReceiver by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        LoginBroadCastReceiver()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val contentView =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        initView(contentView, savedInstanceState)
    }


    fun initView(contentView: ActivityMainBinding, savedInstanceState: Bundle?) {
        contentView.run {
            val navController = findNavController(R.id.nav_host_fragment)
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top level destinations.
            navView.setupWithNavController(navController)
        }
    }


    override fun onPause() {
        super.onPause()
        unregisterReceiver(loginReceiver)
    }

    override fun onResume() {
        super.onResume()
        val filter = IntentFilter()
        filter.addAction(LoginBroadCastReceiver.LOGIN_RECEIVER_ACTION)
        registerReceiver(loginReceiver, filter)
    }
}